import unittest

import requests

from api.recharge_api import RechargeApi
from api.reg_login_api import RegLoginApi
from common.util import build_account, html_parser
from config import path_cfg


class TestRecharge(unittest.TestCase):

    def setUp(self) -> None:
        self.session = requests.Session()
        self.reg_login_api = RegLoginApi(self.session)
        self.recharge_api = RechargeApi(self.session)

        phone = path_cfg.REG_LOGIN_PHONE
        build_account('recharge', self.session, phone)
        self.reg_login_api.user_login({"keywords": phone, "password": "a123456"})

    def tearDown(self) -> None:
        self.session.close()

    # 获取充值验证码
    def test01_get_recharge_verify_code(self):
        # 获取充值图片验证码
        # 断言
        resp = self.recharge_api.get_img_verify_code(1)
        self.assertEqual(200, resp.status_code)

    # 充值
    def test02_recharge(self):
        self.recharge_api.get_img_verify_code(1)

        form_data = {
            "paymentType": "chinapnrTrust",
            "amount": "10000",
            "formStr": "reForm",
            "valicode": "8888"
        }
        resp = self.recharge_api.recharge(form_data)
        self.assertEqual(200, resp.status_code)
        self.assertEqual(200, resp.json().get('status'))

    # 第三方充值
    def test03_third_recharge(self):
        self.recharge_api.get_img_verify_code(1)

        form_data = {
            "paymentType": "chinapnrTrust",
            "amount": "10000",
            "formStr": "reForm",
            "valicode": "8888"
        }
        resp = self.recharge_api.recharge(form_data)

        form_data = resp.json().get('description').get('form')
        parser_data = html_parser(form_data)
        third_recharge_resp = self.recharge_api.third_recharge(parser_data.get('url'), parser_data.get('req_body'))
        self.assertIn('OK', third_recharge_resp.text)
